//index.js

const app = getApp();

Page({
  data: {
    pageturn: false,
    downTime: 15,
    AllTimeStr: "00:00",
    leftTime: 0,
    uptime: 0,
    continuetime: 0,
    navBarHeight: app.globalData.navBarHeight,
    menuTopSpace: app.globalData.menuTopSpace,
    menuRightSpace: app.globalData.menuRightSpace,
    menuHeight: app.globalData.menuHeight,
    menuWidth: app.globalData.menuWidth,
    statusBarHeight: app.globalData.statusBarHeight,
    startBreath: "",
    start: "start",
    type: "up",
    upchose: "chosed",
    downchose: "",
    hideBar: "",
    keeptime: true,
    hourArr: [],
    minuteArr: [],
    hourindex: 0,
    minuteindex: 0,
    isShow: "displayNone"
  },

  quit() {
    this.setData({
      pageturn: false
    })
  },


  uptype() {
    this.setData({
      type: "up",
      upchose: "chosed",
      downchose: "",
      keeptime: true
    })
  },

  bindHourChange(e) {
    console.log(e.detail.value);
    this.setData({
      hourindex: e.detail.value
    })
  },

  bindMinuteChange(e) {
    console.log(e.detail.value);
    this.setData({
      minuteindex: e.detail.value
    })
  },

  downtype() {
    this.setData({
      type: "down",
      upchose: "",
      downchose: "chosed",
      keeptime: false
    })
  },

  start() {
    this.setData({
      start: "stop"
    })
    this.setData({
      startBreath: "startBreath",
      hideBar: "hideBar",
      isShow: ""
    })
    if(this.data.type === 'up') {
      this.timeInterval = setInterval(this.up , 1000)
    } else if (this.data.type === "down") {
      let leftTime = Number(this.data.hourArr[this.data.hourindex]) * 60 * 60000 + Number(this.data.minuteArr[this.data.minuteindex]) * 60000;
      let AllTimeStr = this.format(leftTime);
      this.setData({
        leftTime,
        keeptime: true,
        AllTimeStr
      })
      this.timeInterval = setInterval(this.down, 1000)
    }
  },

  stop() {
    this.setData({
      start: "continue",
      isShow: "displayNone"
    })
    this.setData({
      startBreath: ""
    })
    clearInterval(this.timeInterval);
    let arr = this.data.AllTimeStr.split(":");
    if(arr.length === 2) {
      this.setData({
        continuetime: Number(arr[0]) * 60000 + Number(arr[1]) * 1000
      })
    } else if(arr.length === 3) {
      this.setData({
        continuetime: Number(arr[0]) * 60 * 60000 + Number(arr[1]) * 60000 + Number(arr[2]) * 1000
      })
    }
  },

  continue() {
    this.setData({
      start: "stop"
    })
    this.setData({
      startBreath: "startBreath",
      isShow: ""
    })
    if(this.data.type === 'up') {
      this.timeInterval = setInterval(this.upContinueTime , 1000)
    } else if (this.data.type === "down") {
      this.timeInterval = setInterval(this.downContinueTime, 1000)
    }
  },

  finish() {
    this.setData({
      start: "start"
    })
    this.setData({
      startBreath: "",
      hideBar: ""
    })
    clearInterval(this.timeInterval)
    this.setData({AllTimeStr: "00:00"})
    if(this.data.type === "down") {
      console.log("点击了结束");
      this.setData({
        keeptime: false,
        hourindex: 0,
        minuteindex: 0,
        pageturn: false
      })
    }
    
  },

  typeChange() {
    this.setData({
      pageturn: !this.data.pageturn
    })
  },

  timeInterval: null,

  onLoad: function () {
    let hour, minute, str;
    console.log(app.globalData.openid);
    hour = [];
    minute = [];
    str = "";
    for(let i = 0 ; i < 24 ; i++) {
      str = i < 10 ? "0" + i : i + ''
      hour[hour.length] = str;
    }
    for(let i = 0 ; i < 60 ; i++) {
      str = i < 10 ? "0" + i : i + ''
      minute[minute.length] = str;
    }
    console.log(hour);
    console.log(minute);
   this.setTimeRunner();
   this.setData({
     leftTime: this.data.downTime * 60000,
     hourArr: hour,
     minuteArr: minute
   })
  },

  setTimeRunner: function () {
    let that = this;
    this.timeRunner = setInterval(function () {
      that.getTimeStr();
      that.timeArr();
    }, 1000);
  },

   getTimeStr: function () {
    let that = this;
    var time = new Date();
    var hour = ('00' + time.getHours()).slice(-2);
    var minute = ('00' + time.getMinutes()).slice(-2);
    var second = ('00' + time.getSeconds()).slice(-2);
    var timeStr = (hour + minute + second).split('');
    // timeStr = this.data.AllTimeStr;
    var arr = this.data.AllTimeStr.split(':');
    var Arr;
    if(arr.length === 2) {
      Arr = [...["0", "0"] , ...arr[0].split("") , ...arr[1].split("")]
    } else if(arr.length === 3) {
      Arr = [...arr[0].split("") , ...arr[1].split(""), ...arr[2].split("")]
    }
    that.setData({timeStr:Arr})
  },

  timeArr: function () {
    let that = this;
    var timeArr = this.data.timeStr.map(function (unit, index) {
      var max;
      if (index & 1 == 1) { 
        max = 9;
      } else if (index == 0) { 
        max = 2;
      } else if (index == 2) { 
        max = 5;
      } else if (index == 4) { 
        max = 5;
      }
      var current = Number(unit)
      that.setData({
        max: max,
        current: current,
      })
      return {
        max: max,
        current: current
      };
    })
    // console.log(timeArr);
    that.setData({timeArr:timeArr})
  },


  beforeDestroy:function () {
    clearInterval(this.timeRunner);
  },

  format(leftTime) {
    let hours, minutes, seconds, AllTimeStr;
    hours = Math.floor(leftTime / 1000 / 60 / 60 % 24);
    minutes = Math.floor(leftTime / 1000 / 60 % 60);
    seconds = Math.floor(leftTime / 1000 % 60);
    seconds = seconds < 10 ? "0" + seconds : seconds
    minutes = minutes < 10 ? "0" + minutes : minutes
    if(hours === 0) {
      AllTimeStr = minutes + ":" + seconds;
    } else if(hours > 0) {
      hours = hours < 10 ? "0" + hours : hours
      AllTimeStr = hours + ":" + minutes + ":" + seconds;
    }
    return AllTimeStr;
  },

  down() {
    this.setData({leftTime: this.data.leftTime - 1000})
    this.setData({AllTimeStr: this.format(this.data.leftTime)})
    console.log(this.data.leftTime);
    if(this.data.leftTime <= 0) {
      this.finish();
      this.setData({
        hourindex: 0,
        minuteindex: 0
      })
    }
  },

  up() {
    this.setData({
      uptime: this.data.uptime + 1000
    })
    this.setData(
      {AllTimeStr: this.format(this.data.uptime)}
    )
  },

  upContinueTime() {
    this.setData({
      continuetime: this.data.continuetime + 1000
    })
    this.setData(
      {AllTimeStr: this.format(this.data.continuetime)}
    )
  },

  downContinueTime() {
    this.setData({
      continuetime: this.data.continuetime - 1000
    })
    this.setData(
      {AllTimeStr: this.format(this.data.continuetime)}
    )
  },

  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    this.beforeDestroy()
    // 页面关闭
  },
  onPullDownRefresh: function(){
    wx.stopPullDownRefresh()
  },
})